var e = getApp();

Page({
    data: {
        language: "",
        order_id: "",
        refund_id: "",
        refundTypes: [],
        money: 0,
        typeData: 1,
        fileList1: [],
        isValue: "",
        order_status: 0,
        returnAllGoodsList: [],
        returnGoolsList: [],
        returnTypeItem: {},
        receivingStatus: "not_received",
        returnMethods: 0,
        send_back_way: [],
        returnReasons: "",
        returnExplain: "",
        jdTakeParts: null,
        jd_weight: "",
        jd_length: "",
        jd_width: "",
        jd_height: "",
        start_time: "",
        end_time: "",
        jd_address: {
            pick_up_name: "",
            pick_up_tel: "",
            pick_up_province: 0,
            pick_up_city: 0,
            pick_up_district: 0,
            pick_up_street: 0,
            pick_up_address: "",
            addressName: ""
        },
        refundable_freight: 0,
        refundable_other: 0,
        freight_price: "",
        other_price: "",
        showInstructions: !1,
        pluginid: "",
        orderType: "",
        refundData: {}
    },
    onLoad: function(e) {
        e.order_id && this.setData({
            refund_id: e.order_id,
            order_id: e.order_id
        }), e.value && this.setData({
            isValue: e.value
        }), e.type && this.setData({
            typeData: e.type
        }), e.refundType && this.setData({
            refundType: e.refundType
        }), e.pluginid && this.setData({
            pluginid: e.pluginid
        }), e.orderType && this.setData({
            orderType: e.orderType
        }), e.refundData && this.setData({
            refundData: e.refundData
        }), 2 == e.type ? (wx.setNavigationBarTitle({
            title: "修改申请"
        }), this.getAlterInfo()) : (wx.setNavigationBarTitle({
            title: "售后申请"
        }), this.getRefundInfo());
    },
    onReady: function() {
        var e = wx.getStorageSync("langIndex");
        this.setData({
            language: e.en
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    getAlterInfo: function() {
        var t = this, a = e.getNetAddresss("refund.edit");
        a += "&refund_id=" + this.data.order_id, e._getNetWork({
            url: a,
            success: function(a) {
                var r = a.data;
                if (1 == r.result) {
                    var i = r.data, d = {};
                    d.refundable_freight = Number(i.refundable_freight) || 0, d.refundable_other = Number(i.refundable_other) || 0, 
                    d.freight_price = Number(i.refundApply.freight_price) || "", d.other_price = Number(i.refundApply.other_price) || "", 
                    d.money = Number(i.refundApply.apply_price).toFixed(2), d.refundTypes = i.refundTypes, 
                    d.send_back_way = i.send_back_way, 1 == i.refundApply.receive_status ? d.receivingStatus = "received" : d.receivingStatus = "not_received", 
                    t.selectComponent(".receivingStatus").setRadio(i.refundApply.receive_status), d.returnTypeItem = d.refundTypes[i.refundApply.refund_type], 
                    d.returnReasons = i.refundApply.reason, d.returnExplain = i.refundApply.content, 
                    d.fileList1 = i.refundApply.images, d.returnMethods = i.refundApply.refund_way_type, 
                    t.selectComponent(".returnMethods").setRadio(d.returnMethods), d.returnGoolsList = i.refundApply.refund_order_goods, 
                    d.order_status = i.refundApply.order.status, 1 == d.returnMethods && i.send_back_way_data ? t.getJdTakeParts(i.send_back_way_data) : 1 == d.returnMethods && t.getJdTakeParts(), 
                    t.setData(d);
                } else e.tips(r.msg);
            },
            fail: function(e) {
                console.log(e);
            }
        });
    },
    getRefundInfo: function() {
        var t = this, a = e.getNetAddresss("refund.apply");
        if (e._isTextEmpty(this.data.refundType) || "installationSever" != this.data.refundType || (a = e.getNetAddresss("plugin.live-install.frontend.apply.index")), 
        e._isTextEmpty(this.data.isValue) || 54 != this.data.isValue || (a = e.getNetAddresss("plugin.blind-box.api.apply.index")), 
        console.log(this.data.refundType), 4 == this.data.typeData && (a = e.getNetAddresss("plugin.travel-around.frontend.refund.refund"), 
        "travelAround" == this.data.orderType)) {
            var r = JSON.parse(this.data.refundData);
            this.setData({
                returnReasons: r.reason,
                returnExplain: r.content,
                "returnTypeItem.name": r.refund_type_name
            });
        }
        a += "&order_id=" + this.data.order_id, e._getNetWork({
            url: a,
            success: function(a) {
                var r = a.data;
                if (1 == r.result) {
                    var i, d, s, n = r.data;
                    if (console.log(n), t.setData({
                        refundable_freight: Number(n.refundable_freight) || 0,
                        refundable_other: Number(n.refundable_other) || 0,
                        freight_price: Number(n.refundable_freight) || 0,
                        other_price: Number(n.refundable_other) || 0,
                        money: Number(n.order.price).toFixed(2) || Number(null == n || null === (i = n.order) || void 0 === i ? void 0 : i.order_price).toFixed(2),
                        refundTypes: n.refundTypes,
                        order_status: n.order.status
                    }), console.log(t.data.money), console.log(null == n || null === (d = n.order) || void 0 === d ? void 0 : d.order_price), 
                    t.data.order_status >= 1 && n.support_batch ? t.openComponentsPop("returnGoods") : "travelAround" !== t.data.orderType && t.openComponentsPop("returnType"), 
                    154 == t.data.pluginid) t.setData({
                        money: Number(null == n || null === (s = n.order) || void 0 === s ? void 0 : s.order_price).toFixed(2)
                    });
                    if (154 != t.data.pluginid && t.setData({
                        send_back_way: n.send_back_way,
                        returnMethods: null != n && n.send_back_way[0] ? n.send_back_way[0].value : 0,
                        returnAllGoodsList: n.order.order_goods.filter(function(e) {
                            return e.stepperValue = 1, 0 != e.refundable_total;
                        })
                    }), "travelAround" === t.data.orderType) {
                        var o = JSON.parse(t.data.refundData);
                        console.log(o), t.setData({
                            returnTypeItem: t.data.refundTypes[o.refund_type]
                        });
                    }
                } else e.tips(r.msg);
            },
            fail: function(e) {
                console.log(e);
            }
        });
    },
    confirmRefund: function() {
        if (2 != this.data.returnTypeItem.value && "" == this.data.returnReasons) return e.tips("请选择申请原因！");
        if (this.data.refundable_freight > 0 && Number(this.data.freight_price) > this.data.refundable_freight) return e.tips("退款运费金额最大为".concat(this.data.refundable_freight));
        if (this.data.refundable_other > 0 && Number(this.data.other_price) > this.data.refundable_other) return e.tips("退款其他金额最大为".concat(this.data.refundable_other));
        var t = {
            order_id: this.data.order_id,
            refund_id: this.data.refund_id,
            refund_type: this.data.returnTypeItem.value,
            reason: this.data.returnReasons,
            content: this.data.returnExplain,
            receive_status: "not_received" == this.data.receivingStatus ? 0 : 1,
            refund_way_type: this.data.returnMethods,
            freight_price: this.data.freight_price || 0,
            other_price: this.data.other_price || 0
        };
        if (this.data.order_status > 0) {
            var a = [];
            this.data.returnGoolsList.forEach(function(e) {
                var t = {};
                t.id = e.id, t.total = e.refund_total, a.push(t);
            }), a.length > 0 && (t.order_goods = a), t.part_refund = 1;
        }
        if (1 == this.data.returnMethods) {
            if (!this.data.start_time || !this.data.end_time) return e.tips("请选择取件时间");
            Object.assign(t, this.data.jd_address), t.jd_weight = this.data.jd_weight, t.jd_length = this.data.jd_length, 
            t.jd_width = this.data.jd_width, t.jd_height = this.data.jd_height, t.start_time = this.data.start_time, 
            t.end_time = this.data.end_time;
        }
        var r = "";
        if (1 == this.data.typeData) r = e.getNetAddresss("refund.apply.store"); else if (3 == this.data.typeData) r = e.getNetAddresss("plugin.snatch-regiment.api.order.refund"); else if (4 == this.data.typeData) {
            if (r = e.getNetAddresss("plugin.travel-around.frontend.refund.refund.apply"), t.price = Number(this.data.money), 
            t.refund_id = "", "travelAround" === this.data.orderType) {
                var i = JSON.parse(this.data.refundData);
                t.refund_id = i.id, console.log(i);
            }
        } else r = e.getNetAddresss("refund.edit.store");
        e._isTextEmpty(this.data.refundType) || "installationSever" != this.data.refundType || (r = e.getNetAddresss("plugin.live-install.frontend.apply.store")), 
        e._isTextEmpty(this.data.isValue) || 54 != this.data.isValue || (r = e.getNetAddresss("plugin.blind-box.api.apply.store")), 
        this.data.fileList1.length > 0 && (t.images = JSON.stringify(this.data.fileList1)), 
        e._postNetWork({
            url: r,
            showToastIn: !1,
            data: t,
            success: function(t) {
                var a = t.data;
                1 == a.result ? wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1500,
                    success: function() {
                        setTimeout(function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    }
                }) : e.tips(a.msg);
            },
            fail: function(e) {}
        });
    },
    updateImages: function(e) {
        this.setData({
            fileList1: e.detail
        });
    },
    openComponentsPop: function(e) {
        var t = e.currentTarget ? e.currentTarget.dataset.name : e;
        this.selectComponent(".".concat(t)).setPopShow();
    },
    confrimReturnType: function(e) {
        var t = e.detail.item;
        if (this.data.returnTypeItem.value != t.value) {
            var a = {};
            0 != t.value && (a.receivingStatus = "received"), a.returnTypeItem = t, a.returnReasons = "", 
            this.setData(a);
        }
    },
    confirmReturnReason: function(e) {
        var t = e.detail.text;
        this.setData({
            returnReasons: t
        });
    },
    confrimReturnGoods: function(e) {
        this.setData({
            returnGoolsList: e.detail.modelsArr,
            money: e.detail.total_price
        }), this.openComponentsPop("returnType");
    },
    confirmReceivingStatus: function(e) {
        this.setData({
            receivingStatus: e.detail.status
        });
    },
    confirmReturnMethods: function(e) {
        this.setData({
            returnMethods: e.detail.status
        }), 1 == this.data.returnMethods && null == this.data.jdTakeParts && this.getJdTakeParts();
    },
    confirmPickUpTime: function(e) {
        this.setData({
            start_time: e.detail.start_time,
            end_time: e.detail.end_time
        });
    },
    confirmEditAddress: function(e) {
        this.setData({
            jd_address: e.detail
        });
        var t = {
            province_id: e.detail.pick_up_province,
            city_id: e.detail.pick_up_city,
            district_id: e.detail.pick_up_district,
            street_id: e.detail.pick_up_street,
            address: e.detail.pick_up_address
        };
        this.getPickUpTime(t);
    },
    getJdTakeParts: function() {
        var t = this, a = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : null, r = e.getNetAddresss("plugin.jd-take-parts.frontend.refund.get-default-data");
        e._postNetWork({
            url: r,
            data: {
                order_id: this.data.order_id
            },
            success: function(r) {
                var i = r.data;
                if (1 == i.result) if (null == a) t.setData({
                    jdTakeParts: i.data,
                    jd_height: i.data.default.default_height,
                    jd_length: i.data.default.default_length,
                    jd_weight: i.data.default.default_weight,
                    jd_width: i.data.default.default_width
                }), t.selectComponent(".editAddress").setFormData(i.data.order_address), t.getPickUpTime(i.data.order_address); else {
                    t.setData({
                        jdTakeParts: i.data,
                        jd_height: a.jd_height,
                        jd_length: a.jd_length,
                        jd_weight: a.jd_weight,
                        jd_width: a.jd_width,
                        start_time: a.start_time,
                        end_time: a.end_time
                    });
                    var d = {
                        realname: a.pick_up_name,
                        mobile: a.pick_up_tel,
                        province: a.pick_up_province_name,
                        city: a.pick_up_city_name,
                        district: a.pick_up_district_name,
                        street: a.pick_up_street_name,
                        province_id: a.pick_up_province,
                        city_id: a.pick_up_city,
                        district_id: a.pick_up_district,
                        street_id: a.pick_up_street,
                        address: a.pick_up_address
                    };
                    t.selectComponent(".editAddress").setFormData(d), t.getPickUpTime(d);
                } else e.tips(i.msg);
            },
            fail: function(e) {}
        });
    },
    getPickUpTime: function(t) {
        var a = this, r = {
            pick_up_province: t.province_id,
            pick_up_city: t.city_id,
            pick_up_district: t.district_id,
            pick_up_street: t.street_id,
            pick_up_address: t.address
        }, i = e.getNetAddresss("plugin.jd-take-parts.frontend.refund.get-pickup-in-time");
        e._postNetWork({
            url: i,
            data: r,
            success: function(t) {
                var r = t.data;
                1 == r.result ? a.selectComponent(".pickUpTime").setListData(r.data.time_list) : e.tips(r.msg);
            },
            fail: function(e) {}
        });
    },
    showInstructionsFn: function() {
        this.setData({
            showInstructions: !0
        });
    },
    onCloseInstructions: function() {
        this.setData({
            showInstructions: !1
        });
    },
    onChangeFreightPrice: function(e) {
        this.setData({
            freight_price: e.detail
        });
    },
    onChangeOtherPrice: function(e) {
        this.setData({
            other_price: e.detail
        });
    }
});